package ie.hurley.service.impl;


import java.util.Map;

import com.opensymphony.xwork2.ActionContext;

import 	ie.hurley.model.teachers;
import 	ie.hurley.service.LoginService;
import	ie.hurley.dao.TeacherDao;
import	ie.hurley.dao.StudentDao;
public class loginServiceImpl implements LoginService {
	private TeacherDao teacherDao;
	private StudentDao studentDao;
	public TeacherDao getTeacherDao() {
		return teacherDao;
	}
	public void setTeacherDao(TeacherDao teacherDao) {
		this.teacherDao = teacherDao;
	}
	public StudentDao getStudentDao() {
		return studentDao;
	}
	public void setStudentDao(StudentDao studentDao) {
		this.studentDao = studentDao;
	}
	@Override
	public String login(String username, String password, String type) {
		// TODO Auto-generated method stub
		if(type.equals("teacher")){
			int teacher_id = Integer.parseInt(username);
			int teas = teacherDao.findteacher(teacher_id, password);
			if(teas>0){
				Map<String,Object> sess = ActionContext.getContext().getSession();
				teachers loginer = teacherDao.findteacherbyid(teacher_id);
				sess.put("login", loginer);
				return "teacher";
			}
			else{
				return "failed";
			}
		}
		else if(type.equals("student")){
			int stu = username.hashCode();
			int teas = studentDao.findstudent(stu, password);
			if(teas>0){
				return "student";
			}
			else{
				return "failed";
			}
		}
		else if(type.equals("manager")){
			return "manager";
		}
		else {
			return "unknow";
		}
	}
	
}
